import { AuthGuard } from "@/auth";
import Home from "@/pages/home/index.vue";
import Login from "@/pages/login/index.vue";
import VueRouter from "vue-router";

const routes = [
  {
    /**
     * path: 路径为 / 时触发该路由规则
     * name: 路由的 name 为 Home
     * component: 触发路由时加载 `Home` 组件
     */
    path: "/",
    name: "Home",
    component: Home,
    beforeEnter: (to: any, from: any, next: any) => {
      const auth: Promise<boolean> = AuthGuard.checkAuth(router);
      auth.then((result) => {
        next(result ? true : false);
      }).catch(() => {
        next(false);
      });
    },
    meta: {
      caption: '基座首页'
    }
  },
  {
    /**
     * path: 路径为 / 时触发该路由规则
     * name: 路由的 name 为 Login
     * component: 触发路由时加载 `Login` 组件
     */
    path: "/login",
    name: "Login",
    component: Login,
  }
];

/**
 * 注册路由实例
 * 即将开始监听 location 变化，触发路由规则
 */
const router = new VueRouter({
  mode: "history",
  routes,
});

export default router;
